/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pras.skripsi.sql;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author wisnu
 */
public class DataPasienDao {

    private final Connection connection;

    private final String insertData = "INSERT INTO `data_pasien` (tgl_tes,nama,alamat,tgl_lahir,jenis_kelamin,gol_darah,hasil) VALUES (?,?,?,?,?,?,?)";
    private final String updateData = "UPDATE `data_pasien` SET nama = ?, alamat = ?, tgl_lahir = ?, jenis_kelamin = ?, gol_darah = ?, hasil = ? WHERE id_pasien = ?";
    private final String deleteData = "DELETE FROM `data_pasien` WHERE id_pasien = ?";
    private final String selectAll = "SELECT * FROM `data_pasien`";
    private final String selectByDate = "SELECT * FROM `data_pasien` WHERE `tgl_tes` = ?";

    public DataPasienDao(Connection connection) {
        this.connection = connection;
    }

    public void insertData(DataPasien dataPasien) throws Exception {
        PreparedStatement statement = null;

        try {
            statement = connection.prepareStatement(insertData, Statement.RETURN_GENERATED_KEYS);
            statement.setString(2, dataPasien.nama);
            statement.setDate(1, new Date(dataPasien.getTanggalTes().getTime()));
            statement.setString(3, dataPasien.alamat);
            statement.setString(4, dataPasien.tanggalLahir);
            statement.setString(5, dataPasien.jenisKelamin);
            statement.setString(6, dataPasien.golonganDarah);
            statement.setString(7, dataPasien.hasil);

            statement.executeUpdate();

            ResultSet resultSet = statement.getGeneratedKeys();
            if (resultSet.next()) {
                dataPasien.setIdPasien(resultSet.getInt(1));
            }

        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public void updateData(DataPasien dataPasien) throws Exception {
        PreparedStatement statement = null;

        try {
            statement = connection.prepareStatement(updateData);
            statement.setString(1, dataPasien.nama);
            statement.setString(2, dataPasien.alamat);
            statement.setString(3, dataPasien.tanggalLahir);
            statement.setString(4, dataPasien.jenisKelamin);
            statement.setString(5, dataPasien.golonganDarah);
            statement.setString(6, dataPasien.hasil);
            statement.setInt(7, dataPasien.idPasien);

            statement.executeUpdate();

        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void deleteData(DataPasien dataPasien) throws Exception {
        PreparedStatement statement = null;

        try {
            statement = connection.prepareStatement(deleteData);
            statement.setInt(1, dataPasien.idPasien);

            statement.executeUpdate();

        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public List<DataPasien> selectAll() throws Exception {
        Statement statement = null;
        List<DataPasien> list = new ArrayList<DataPasien>();
        try {
            statement = connection.createStatement();
            ResultSet result = statement.executeQuery(selectAll);

            while (result.next()) {
                DataPasien pasien = new DataPasien();

                pasien.setIdPasien(result.getInt("id_pasien"));
                pasien.setTanggalTes(result.getDate("tgl_tes"));
                pasien.setNama(result.getString("nama"));
                pasien.setAlamat(result.getString("alamat"));
                pasien.setTanggalLahir(result.getString("tgl_lahir"));
                pasien.setJenisKelamin(result.getString("jenis_kelamin"));
                pasien.setGolonganDarah(result.getString("gol_darah"));
                pasien.setHasil(result.getString("hasil"));

                list.add(pasien);
            }

            return list;
        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<DataPasien> selectByDate(java.util.Date date) throws Exception {
        PreparedStatement statement = null;
        List<DataPasien> list = new ArrayList<DataPasien>();
        try {
            statement = connection.prepareStatement(selectByDate);
            statement.setDate(1, new Date(date.getTime()));
            ResultSet result = statement.executeQuery();

            while (result.next()) {
                DataPasien pasien = new DataPasien();

                pasien.setIdPasien(result.getInt("id_pasien"));
                pasien.setTanggalTes(result.getDate("tgl_tes"));
                pasien.setNama(result.getString("nama"));
                pasien.setAlamat(result.getString("alamat"));
                pasien.setTanggalLahir(result.getString("tgl_lahir"));
                pasien.setJenisKelamin(result.getString("jenis_kelamin"));
                pasien.setGolonganDarah(result.getString("gol_darah"));
                pasien.setHasil(result.getString("hasil"));

                list.add(pasien);
            }

            return list;
        } catch (SQLException ex) {
            throw new Exception(ex.getMessage());
        }

    }
}
